<?php

namespace App\Controllers\v1;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Controllers\BaseController;
use App\Services\MoneyService;
use App\Services\ActiveApplyService;
use common\utils\SysCode;

class AccountController extends BaseController
{
    public function getCoupon(Request $request)
    {
        $user = Auth::user();
        $apply = ActiveApplyService::getActiveApply($user->id, $request->all());
        $apply['list'] = ActiveApplyService::formatApply($apply['list']);
        $apply = amount_format_array($apply, ["coupon_amount"]);
        return $apply;
    }

    public function getWallet(Request $request)
    {
        $user = Auth::user();
        $wallet = MoneyService::getWallet($user->id);
        if (!$wallet) {
            return error_exit(SysCode::INTERNAL_SERVER_ERROR);
        }
        $wallet->balance = amount_format($wallet->balance);
        return $wallet;
    }

    public function getWalletPartner(Request $request)
    {
        $this->validate($request, [
            'partner_id' => 'required',
        ]);
        $partner_id = $request->input('partner_id');
        $user = Auth::user();
        $wp = MoneyService::getWalletPartner($user->id, $partner_id);
        if (!$wp) {
            return error_exit(SysCode::INVALID_ARGUMENT);
        }
        $wp->balance = amount_format($wp->balance);
        return $wp;
    }

    public function getPartnerList(Request $request)
    {
        return MoneyService::getPartnerList();
    }

    //一键回收
    public function recoveryPartner()
    {
        $user = Auth::user();
        $data = MoneyService::recoveryWallet($user);
        return $data;
    }
}
